package cn.itcast.server.service;

import cn.itcast.config.Config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: LDeng
 * @Date: 2021-06-10 12:52
 */
public class ServicesFactory {

    static Properties properties;
    static Map<Class<?>,Object> map=new ConcurrentHashMap<>();

    static {
        try {
            InputStream in = Config.class.getResourceAsStream("/application.properties");
            properties=new Properties();
            properties.load(in);
            Set<String> names=properties.stringPropertyNames();
            for(String name:names){
                if(name.endsWith("Service")){
                    Class<?> interfaceClass=Class.forName(name);
                    Class<?> instanceClass=Class.forName(properties.getProperty(name));
                    map.put(interfaceClass,instanceClass.newInstance());
                }
            }
        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new ExceptionInInitializerError(e);
        }

    }

    public static <T> T getService(Class<T> interfaceClass) {
        return (T) map.get(interfaceClass);
    }


}
